package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.StringUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import com.kdtech.crawler.CrawlHTML;

import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
/**
 * 徐闻论坛
 */
public class XwieBbsAnalyse implements AnalyseNews{

	
	public boolean isDetailPage(String url){
		boolean bRet = false;
		String[] regex = {
				"http://bbs.xwie.com/read.php\\?tid=[0-9]*&fid=[0-9]*",
				"http://bbs.xwie.com/read.php\\?fid=[0-9]*&tid=[0-9]*",
				//http://bbs.xwie.com/read.php?tid=98440&fid=62
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta bbs = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		bbs.setUrl(url);
		String title = null;
		String content = null;
		String author = null;
		Long date = null;
		Integer clickNum=0;
		Integer commentNum=0;

		Document doc = Jsoup.parse(htmltxt);
		title =doc.select("h1#J_post_title").text();

		Element firstDiv = doc.select("div#J_posts_list > div[id^=read_]").first();
		if(firstDiv!=null){
			content = HtmlCleaner.getContentHtml(url, firstDiv.select("div.editor_content"));
			date = DateUtils.matchDate(firstDiv.select("span.fl").text());
			author = firstDiv.select("div.name").text();
		}

		try {
			String clickStr = doc.select("span.hits em").text();
			String commentStr = doc.select("span.replies em").text();
			clickNum = Integer.parseInt(clickStr);
			commentNum = Integer.parseInt(commentStr);
		} catch (Exception e) {
			clickNum = 0;
			commentNum = 0;
		}
		if(StringUtils.isBlank(title)){
		}
		bbs.setClickNum(clickNum);
		bbs.setCommentNum(commentNum);
		bbs.setTitle(StringUtils.trimSpace(title));
		bbs.setContent(content);
		bbs.setAuthor(author);
		bbs.setDate(date);
		bbs.setUpdateUrl(url);
		return bbs;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		if(meta!=null){
			String updateUrl = meta.getUpdateUrl();
			if(updateUrl!=null){
				UrlMeta responseToURL = CrawlHTML.responseToURL(updateUrl);
				if(responseToURL!=null){
					String htmltxt = responseToURL.getHtml();
					Document doc = Jsoup.parse(htmltxt);
					Integer clickNum=0;
					Integer commentNum=0;
					try {
						String clickStr = doc.select("span.hits em").text();
						String commentStr = doc.select("span.replies em").text();
						clickNum = Integer.parseInt(clickStr);
						commentNum = Integer.parseInt(commentStr);
					} catch (Exception e) {
						clickNum = 0;
						commentNum = 0;
					}
					meta.setClickNum(clickNum);
					meta.setCommentNum(commentNum);
					return meta;
				}
			}
		}
		return null;
	}
    
	
}
